define(function (require,exports,module) {
    var sTpl = require("./template.html");
    var enter = require("../enter/enter.js");

    var routeRefresh=function ($this,init) {
        if(init){
            var form={
                startDate:'',
                endDate:'',
                flightNum:'',
                flightInfoId:''
            }
            if($this.$route.params.type=='today'){
                form.startDate = new Date().format('yyyy-MM-dd');
                form.endDate = new Date().format('yyyy-MM-dd');
            }else{
                if($this.$route.params.type=='mediumAndLong'){
                    var startDate = new Date(new Date().getTime()+1000*3600*24*7);
                    var endDate = new Date(new Date().getTime()+1000*3600*24*15);
                    form.startDate = startDate.format('yyyy-MM-dd');
                    form.endDate = endDate.format('yyyy-MM-dd');
                }else{
                    if($this.$route.params.type=='normal'){
                        var startDate = new Date();
                        var endDate = new Date(new Date().getTime()+1000*3600*24*7);
                        form.startDate = startDate.format('yyyy-MM-dd');
                        form.endDate = endDate.format('yyyy-MM-dd');
                    }
                }
            }
            form.url=basepath+"/api/flightPlane/query?sort=expectDate&startDate="+form.startDate+"&endDate="+form.endDate+"&flightInfoId="+(form.flightInfoId?form.flightInfoId:"")
            return form;
        }else{
            if($this.$route.params.type=='today'){
                $this.form.startDate = new Date().format('yyyy-MM-dd');
                $this.form.endDate = new Date().format('yyyy-MM-dd')

            }else{
                if($this.$route.params.type=='mediumAndLong'){
                    var startDate = new Date(new Date().getTime()+1000*3600*24*7);
                    var endDate = new Date(new Date().getTime()+1000*3600*24*15);
                    $this.form.startDate = startDate.format('yyyy-MM-dd');
                    $this.form.endDate = endDate.format('yyyy-MM-dd');
                }else{
                    if($this.$route.params.type=='normal'){
                        var startDate = new Date();
                        var endDate = new Date(new Date().getTime()+1000*3600*24*7);
                        $this.form.startDate = startDate.format('yyyy-MM-dd');
                        $this.form.endDate = endDate.format('yyyy-MM-dd');
                    }
                }

            }
            $this.url=basepath+"/api/flightPlane/query?sort=expectDate&startDate="+$this.form.startDate+"&endDate="+$this.form.endDate+"&flightInfoId="+($this.form.flightInfoId?form.flightInfoId:"")
        }
    }

    var VueComponent = Vue.extend({
        props: ['parentData'],
        template: sTpl,
        components:{
            'enters':enter
        },
        data:function () {
            var form=routeRefresh(this,true);
            return {
                form:form,
                planes:[],
                enterOpen:true,
                enterData:{
                    editOrAdd:'add'
                },
                url:form.url,
                preUrl:form.url
            }
        },methods:{
            queryFlightPlane:function () {
                var url=basepath+"/api/flightPlane/query?sort=expectDate&startDate="+(this.form.startDate?this.form.startDate:'')
                    +"&endDate="+(this.form.endDate?this.form.endDate:'')+"&flightInfoId="+(this.form.flightInfoId?this.form.flightInfoId:"");
                this.url=url;
                this.openEnter(false);
                this.preUrl=this.url;
            },
            add:function(){

            },
            handleSelect:function (val) {
                this.form.flightInfoId=val.flightInfoId;
            },
            pickEndDate:function (val) {
                if(!val){
                    val=''
                }
                this.form.endDate= val;
            },
            pickStartDate:function (val) {
                if(!val){
                    val=''
                }
                this.form.startDate= val;
            },
            openEnter:function (val) {
                if(!val){
                    if(this.preUrl==this.url){
                        this.$refs.flightPlane.freshTableList();
                        this.$refs.enters.$refs.formInline.resetFields();
                        this.$refs.enters.closeExcelTable();
                    }
                }
                this.enterOpen=!val;
                $("#panel-activer").prop("checked",val);
                this.enterData.editCallback=function (val) {
                    this.$refs.flightPlane.freshTableList();
                }

            },
            edit:function (scope) {
                this.openEnter(true);
                this.$refs.enters.formInline=scope.row;
                this.enterData.editOrAdd = 'edit';
                var $this= this;

            },
            deletePlanes:function(scope){
                var $this = this;
                this.$confirm('确定要删除么？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    scope.row.status="删除";
                    axios.post(basepath+'/api/flightPlane/save',scope.row)
                        .then(function (response) {
                            $this.$refs.flightPlane.internalData.splice(scope.$index,1);
                            $this.$notify({
                                title: '记录已删除',
                                message: '记录已删除'
                            });
                        });
                });
            }

        },
        watch:{
            '$route.params.type':{
                handler:function(val,oldval){
                    routeRefresh(this,false);
                },
                deep:true
            }
        },
        created:function () {

            var $this=this;
            axios.get("/dam/api/flightInfo/queryFlightInfo?filter=").then(function (response) {
                $this.planes=response.data;
                $this.planes.forEach(function(plane){
                    plane.value= plane.flightNum;
                })
            })
        },
        activated:function () {
            if(this.$route.params.type=="today"){
                messages.$emit('change','今日计划');
            }
        }

    })
    module.exports=VueComponent;
    module.exports.reDefine=function (val) {
        module.exports.type=val;
        return module.exports;
    }

})